iT邦幫忙

2022 iThome 鐵人賽

DAY 26
0
Software Development

用程式寫遊戲給AI玩系列 第 26

實作!雙人射擊遊戲 —— 玩家與怪物的碰撞

  • 分享至 

  • xImage
  •  

上次的內容為,怪物有五個階段的移動方式,這次我們要讓掉下來的怪物和玩家碰撞時,會死亡,而玩家也會扣生命

對不起!先上 code,之後來補文章/images/emoticon/emoticon16.gif

class BattleMode:
    def handle_collisions(self):
        for mob in self.mobs:
            if isinstance(mob, Mob):
                bullets = pygame.sprite.spritecollide(mob, self.player_1P.bullets, True, pygame.sprite.collide_rect_ratio(0.8))
                if bullets:
                    mob.kill()
                    self.player_1P.score += 10
                bullets = pygame.sprite.spritecollide(mob, self.player_2P.bullets, True, pygame.sprite.collide_rect_ratio(0.8))
                if bullets:
                    mob.kill()
                    self.player_2P.score += 10
                hits_dict = pygame.sprite.groupcollide(self.players, mob.bullets, False, True, pygame.sprite.collide_rect_ratio(0.8))
                for player, bullet in hits_dict.items():
                    if isinstance(player, Player):
                        player.shield -= len(bullet) * 10
                players = pygame.sprite.spritecollide(mob, self.players, False, self.collide_attack_rect)
                for player in  players:
                    if mob.is_attack:
                        mob.kill()
                        player.lives -= 1

    def collide_attack_rect(self, mob: Mob, player: Player) -> int:
        mob_hit_rect = pygame.Rect(*mob.attack_rect.topleft, mob.attack_rect.width - 2, mob.attack_rect.height - 2)
        mob_hit_rect.center = mob.attack_rect.center
        player_hit_rect = pygame.Rect(*player.rect.topleft, player.rect.width-2, player.rect.height-2)
        player_hit_rect.center = player.rect.center
        return mob_hit_rect.colliderect(player_hit_rect)

from os import path
import random

import pygame
from mlgame.utils.enum import get_ai_name
from mlgame.view.view_model import create_image_view_data, create_asset_init_data

from src.Bullet import Bullet
from src.game_module.TiledMap import create_construction
from .env import IMAGE_DIR


DOWN_CMD = "DOWN"
UP_CMD = "UP"
RIGHT_CMD = "RIGHT"
LEFT_CMD = "LEFT"
SHOOT_CMD = "SHOOT"
Vec = pygame.math.Vector2


class Player(pygame.sprite.Sprite):
    def update(self, command: dict) -> None:
        """
        更新玩家資料
        :param command:
        :return:
        """
        if self.is_alive:
            self.bullets.update()
            self.used_frame += 1
            self.rect.center += self.vel
            self.act(command[self.id])
            if self.lives <= 0:
                self.is_alive = False
            if self.shield <= 0:
                self.lives -= 1
                self.shield = 100
        else:
            if self.id == "1P":
                self.rect.topleft = (self.play_rect_area.centerx, -100)
            else:
                self.rect.topleft = (self.play_rect_area.centerx, -50)

        for bullet in self.bullets:
            out = bullet.rect.colliderect(self.play_rect_area)
            if not out:
                bullet.kill()


上一篇
實作!雙人射擊遊戲 —— 怪物掉落
下一篇
實作!雙人射擊遊戲 —— 撰寫 MLPlay 積木
系列文
用程式寫遊戲給AI玩30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言